package com.titanrise.crm.mapper;

import com.titanrise.crm.domain.model.SysLoginLog;
import com.titanrise.crm.environment.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-20 22:09
 */
@Mapper
public interface SysLoginLogMapper {

    @ResultType(SysLoginLog.class)
    @Select("SELECT log_id, user_id, name, login_time, logout_time, login_status, " +
            "ip_address, device_info, failure_reason " +
            "FROM tb_sys_login_logs " +
            "WHERE login_time >= NOW() - INTERVAL #{timeLine} DAY " +
            "ORDER BY login_time DESC " +
            "LIMIT #{offset}, 5")
    List<SysLoginLog> selectLogInfo(@Param("timeLine") int timeLine, @Param("offset") int offset);

    @Select("SELECT count(log_id) FROM tb_sys_login_logs WHERE login_time >= NOW() - INTERVAL #{timeLine} DAY")
    int selectLogInfoCount(@Param("timeLine") int timeLine);

    @Insert(value = "INSERT INTO tb_sys_login_logs VALUES(#{log.logId}, #{log.userId}, #{log.name}, #{log.loginTime}, #{log.logoutTime}, #{log.loginStatus}, #{log.ipAddress}, #{log.deviceInfo}, #{log.failureReason})", autoGenerateUUID = true)
    int insertOne(@ParamEntity("log") SysLoginLog log);

    @Update("UPDATE tb_sys_login_logs SET logout_time = #{logoutTime} WHERE log_id = #{logId}")
    int updateLogoutTimeByID(@Param("logId") String logId, @Param("logoutTime") LocalDateTime logoutTime);
}
